(function($){
	var ms = {
		init:function(obj,args){
			return (function(){
				ms.fillHtml(obj,args);
				ms.bindEvent(obj,args);
			})();
		},
		fillHtml:function(obj,args){
			return (function(){
				obj.empty();
				var recordNum=$("#recordNum").val();
				if(recordNum>0){
				if(args.current <= 1 || args.disabled){
					obj.remove(".prevPage");
					obj.append("<li><span class='hover'><<</span></li>");
					obj.append("<li><span class='hover'><</span></li>");
				}else{
					obj.append("<li><a href='javascript:;' class='firstPage'><<</a></li>");
					obj.append("<li><a href='javascript:;' class='prevPage'><</a></li>");
				}
				if(args.current != 1 && args.current >= 4 && args.pageCount != 4){
					obj.append(args.disabled ? "<li><span class='hover'>1</span></li>"
							:"<li><a href='javascript:;' class='tcdNumber'>1</a></li>");
				}
				if(args.current-2 > 2 && args.current <= args.pageCount && args.pageCount > 5){
					obj.append("<li><span class='omitted'>...</span></li>");
				}
				var start = args.current -2,end = args.current+2;
				if((start > 1 && args.current < 4)||args.current == 1){
					end++;
				}
				if(args.current > args.pageCount-4 && args.current >= args.pageCount){
					start--;
				}
				for (;start <= end; start++) {
					if(start <= args.pageCount && start >= 1){
						if(start != args.current){
							obj.append(args.disabled ? "<li><span class='disabled'>"+start+"</span></li>"
									: "<li><a href='javascript:;' class='tcdNumber'>"+ start +"</a></li>");
						}else{
							obj.append("<li><span class='current'>"+ start +"</span></li>");
						}
					}
				}
				if(args.current + 2 < args.pageCount - 1 && args.current >= 1 && args.pageCount > 5){
					obj.append("<li><span class='omitted'>...</span></li>");
				}
				if(args.current != args.pageCount && args.current < args.pageCount -2  && args.pageCount != 4){
					obj.append(args.disabled ? "<li><span class='hover'>"+args.pageCount+"</span></li>"
							: "<li><a href='javascript:;' class='tcdNumber'>"+args.pageCount+"</a></li>");
				}
				if(args.current >= args.pageCount || args.disabled){
					obj.remove(".nextPage");
					obj.append("<li><span class='hover'>></span></li>");
					obj.append("<li><span class='hover'>>></span></li>");
				}else{
					obj.append("<li><a href='javascript:;' class='nextPage'>></a></li>");
					obj.append("<li><a href='javascript:;' class='lastPage'>>></a></li>");
				}
				}
				})();
		},
		bindEvent:function(obj,args){
			return (function(){
				obj.on("click","a.firstPage",function(){
					ms.fillHtml(obj,{"current":1,"pageCount":args.pageCount, "disabled":true});
					if(typeof(args.backFn)=="function"){
						args.backFn(1);
					}
				});
				obj.on("click","a.lastPage",function(){
					ms.fillHtml(obj,{"current":args.pageCount,"pageCount":args.pageCount, "disabled":true});
					if(typeof(args.backFn)=="function"){
						args.backFn(args.pageCount);
					}
				});
				obj.on("click","a.tcdNumber",function(){
					var current = parseInt($(this).text());
					ms.fillHtml(obj,{"current":current,"pageCount":args.pageCount, "disabled":true});
					if(typeof(args.backFn)=="function"){
						args.backFn(current);
					}
				});
				obj.on("click","a.prevPage",function(){
					var current = parseInt(obj.find("span.current").text());
					ms.fillHtml(obj,{"current":current-1,"pageCount":args.pageCount, "disabled":true});
					if(typeof(args.backFn)=="function"){
						args.backFn(current-1);
					}
				})
				obj.on("click","a.nextPage",function(){
					var current = parseInt(obj.find("span.current").text());
					ms.fillHtml(obj,{"current":current+1,"pageCount":args.pageCount, "disabled":true});
					if(typeof(args.backFn)=="function"){
						args.backFn(current+1);
					}
				});
			})();
		}
	}
	$.fn.createPage = function(options){
		var args = $.extend({
			pageCount : 10,
			current : 1,
			backFn : function(){}
		},options);
		ms.init(this,args);
	}
})(jQuery);